// 云函数入口文件
const cloud = require('wx-server-sdk')

cloud.init({
  env: cloud.DYNAMIC_CURRENT_ENV
});

const db = cloud.database();
const _ = db.command;
const $ = db.command.aggregate;

// 云函数入口函数
exports.main = async (event, context) => {

  function buildPipeLineAllParamsObject (allLookupParams) {
    let pipelineAllParamsObj = {};

    allLookupParams.forEach ((eachRequest) => {
      let eachRequestPipelineArrayList = eachRequest.pipeline;

      let pipeLineAggregatedCommandArray = [];
      eachRequestPipelineArrayList.forEach(pipelineItem => {
        pipeLineAggregatedCommandArray.push($[pipelineItem.operator]([pipelineItem.fieldName, pipelineItem.definedName]));
      });

      pipelineAllParamsObj[eachRequest.name] = pipeLineAggregatedCommandArray;
    });

    return pipelineAllParamsObj;
  }

  function queryAggregatedData(queryRequest) {
    var originalCollectionName = queryRequest.originCollection;
    var aggregatedAction = db.collection(originalCollectionName)
                           .aggregate();

    let allLookupParams = queryRequest.fromCollectionsLookUpParmas;

    let transferedPipelineParamsObj = buildPipeLineAllParamsObject(allLookupParams);

    return new Promise((resolve, reject) => {
      allLookupParams.forEach(function(eachRequest) {
        let name = eachRequest.name;
        
        aggregatedAction.lookup({
          from: name,
          let: eachRequest.let,
          pipeline: $.pipeline()
            .match(_.expr($.and(
              transferedPipelineParamsObj[name]
            )))
            .project(
              eachRequest.project
            )
            .done(),
          as: eachRequest.as
          })
      });

      resultSet = aggregatedAction
                  .sort({
                    [queryRequest.orderBy] : 1
                  })
                  .limit(queryRequest.limit)
                  .end()
                  .then(res => {
                    let resultSet = res.list;
                    console.log(`[数据库] AggregatedLookUpWithSortAndLimitQuery : ${originalCollectionName} [查询记录] 成功: `, resultSet);
                    
                    resolve(resultSet);
                  });
    });
  }

  return queryAggregatedData(event.queryRequest);
}

/*
  The structures of "pipelineAllParamsObj" & "pipeLineAggregatedCommandArray"

   {
    collectionA : [
                  {
                    operator : 'eq', 
                    fieldName : '$userID',
                    definedName : '$$userID'
                  },
                  {
                    operator : 'gt', 
                    fieldName : '$userID',
                    definedName : 2
                  },                                
                ],
    collectionB : [
                  {
                    operator : 'eq', 
                    fieldName : '$identityID',
                    definedName : '$$identityID'
                  },
                  {
                    operator : 'gt', 
                    fieldName : '$identityID',
                    definedName : 1
                  },                                
                ],
   }

Refactor to below format :

  Collection A :
       pipeline: $.pipeline()
        .match(_.expr($.and([
          $.eq(['$userID', '$$userID']),
          $.gt(['$userID', 2])
        ])))
        
        ...

  Collection B :
        pipeline: $.pipeline()
        .match(_.expr($.and([
          $.eq(['$userID', '$$userID']),
          $.gt(['$userID', 2])
        ])))
 */